<template>
    <div class="edu-page-warp">
        <el-card shadow="never">
            <div class="edu-page-title">{{$t('Parameter.ParameterSetting')}}</div>
            <el-form :model="parameter" ref="parameter" label-suffix=":">
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            :label="$t('label.college')"
                            prop="faculty"
                            v-if="tutorType == 2 "
                            :rules="[
                        { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                      ]"
                        >
                            <DictSelect
                                v-model="parameter.faculty"
                                :data="options.faculty"
                                style="width: 60%;"
                                @change="getFaculty"
                                :clearable="false"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="学院导师配比" prop="number" v-if="tutorType == 2 ">
                            <el-input-number
                                v-model="parameter.number"
                                controls-position="right"
                                :min="1"
                                style="width: 60%;"
                            ></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="新生导师配比" prop="number" v-if="tutorType == 1 ">
                            <el-input-number
                                v-model="parameter.number"
                                controls-position="right"
                                :min="1"
                                style="width: 60%;"
                            ></el-input-number>
                        </el-form-item>
                    </el-col>
                    <div class="float-right">
                        <el-button @click="reset" v-premit="reset_Parameter">{{$t('message.reset')}}</el-button>
                        <el-button
                            @click="save"
                            type="primary"
                            v-premit="save_Parameter"
                        >{{$t('common.btn.save')}}</el-button>
                    </div>
                </el-row>
            </el-form>
        </el-card>
        <el-card shadow="never">
            <div class="edu-page-title">{{$t('Parameter.special')}}</div>
            <div class="edu-opertion-bar">
                <div class="float-left">
                    <KeywordInput
                        v-model="queryParams.condition"
                        @enter="searchList"
                        placeholder="请输入教师工号或姓名搜索"
                    ></KeywordInput>
                </div>
                <div class="float-right">
                    <el-button
                        @click="updateRecord(null)"
                        type="primary"
                        v-premit="add_special"
                    >{{$t('common.btn.add')}}</el-button>
                </div>
            </div>
            <edu-table
                url="/api/welcomeservice/tutorNumber/list"
                ref="mainTable"
                :order="true"
                :selection="false"
            >
                <el-table-column :label="$t('Parameter.jobNumber')" prop="teacherCode" />
                <el-table-column :label="$t('label.stuName')" prop="teacherName" />
                <el-table-column
                    :label="$t('Parameter.personnelCollenge')"
                    prop="personnelCollegeI18n"
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('Parameter.tutorCollenge')"
                    prop="facultyI18n"
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('label.grade')"
                    prop="grade"
                    v-if="tutorType == 1 "
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('school.school')"
                    prop="school"
                    v-if="tutorType == 1 "
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('Parameter.schoolMajor')"
                    prop="majorI18n"
                    v-if="tutorType == 1 "
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('label.major')"
                    prop="majorI18n"
                    v-if="tutorType == 2 "
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('Parameter.title')"
                    prop="titleI18n"
                    show-overflow-tooltip
                />
                <el-table-column :label="$t('Parameter.tutorNumber')" prop="number" />
                <el-table-column :label="$t('message.operating')" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="updateRecord(scope.row)"
                            size="mini"
                            v-premit="edit_special"
                        >{{$t('message.edit')}}</el-button>
                        <el-button
                            type="text"
                            @click="deletelRecord(scope.row)"
                            size="mini"
                            v-premit="delete_special"
                        >{{$t('message.delete')}}</el-button>
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>

        <edu-dialog
            :title="dialogTitle"
            width="700px"
            ref="updateDialog"
            :auto-height="true"
            @close="resetAddForm"
        >
            <el-form
                :model="createForm"
                ref="createForm"
                label-width="100px"
                label-suffix=":"
                :rules="rules"
            >
                <el-row>
                    <el-col :span="12">
                        <el-form-item :label="$t('Parameter.jobNumber')" prop="teacherCode">
                            <el-input
                                v-model="createForm.teacherCode"
                                :disabled="isShow"
                                @change="getTeacherfo"
                            >
                                <!-- 	<el-button slot="append" @click="getTeacherfo" icon="el-icon-search" :disabled="isShow"></el-button> -->
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('label.stuName')" prop="teacherName">
                            <el-input v-model="createForm.teacherName" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                            :label="$t('Parameter.personnelCollenge')"
                            prop="personnelCollege"
                        >
                            <DictSelect
                                v-model="createForm.personnelCollege"
                                :data="options.faculty"
                                :disabled="true"
                                valueAll
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('Parameter.tutorCollenge')" prop="faculty">
                            <DictSelect
                                v-model="createForm.faculty"
                                :data="options.faculty"
                                :disabled="true"
                                valueAll
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('Parameter.title')" prop="title">
                            <DictSelect
                                v-model="createForm.title"
                                :data="options.title"
                                :disabled="true"
                                valueAll
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                            :label="$t('label.grade')"
                            prop="grade"
                            v-if="tutorType == 1 "
                        >
                            <el-input v-model="createForm.grade" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                            :label="$t('school.school')"
                            prop="school"
                            v-if="tutorType == 1 "
                        >
                            <el-input v-model="createForm.school" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                            :label="$t('Parameter.schoolMajor')"
                            prop="major"
                            v-if="tutorType == 1 "
                        >
                            <DictSelect
                                v-model="createForm.major"
                                @change="getTeacher(true)"
                                :data="options.major"
                                :clearable="false"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('label.major')" v-if="tutorType == 2 ">
                            <DictSelect
                                v-model="createForm.major"
                                @change="getTeacher(false)"
                                :data="options.major"
                                :clearable="false"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('Parameter.tutorMatchingNumber')" prop="number">
                            <el-input-number
                                v-model="createForm.number"
                                controls-position="right"
                                :min="1"
                                style="width:100%"
                                maxlength="11"
                            ></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="buttons">
                <el-button @click="close(createForm)">{{$t('message.cancel')}}</el-button>
                <el-button @click="submitForm" type="primary">{{$t('message.submit')}}</el-button>
            </span>
        </edu-dialog>
        <el-dialog title="系统提示" :visible.sync="dialogVisible" width="400px">
            <span>返回将不会保存当前信息激活设置，是否确定？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancle">取 消</el-button>
                <el-button type="primary" @click="summit">确 定</el-button>
                <el-button type="primary" @click="saveClose">保存并关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import TutorAPIS from 'common/src/api/welcomeService/tutorList'
import APIs from 'common/src/api/welcomeService/schoolRelation'
import UTILS from 'common/src/utils/utils'

const defaultCreateForm = {
    teacherCode: '',
    teacherName: '',
    personnelCollege: '',
    faculty: '',
    major: '',
    title: '',
    number: 1,
    type: ''
}
// 本科生导师配比
export default {
    name: 'TutorNumber',
    props: ['tutorType', 'currencyType'],
    computed: {},
    mounted() {
        this.getOptions()
    },
    data() {
        return {
            dialogTitle: '',
            isShow: false,
            dialogVisible: false,
            currencyList: [],
            options: {
                faculty: [],
                title: [],
                major: []
            },
            teachers: [],
            queryParams: {
                condition: '',
                type: ''
            },
            parameter: {
                faculty: '',
                number: 1,
                type: ''
            },
            rules: {
                teacherCode: [
                    {
                        required: true,
                        validator: this.checkTeacherCode,
                        trigger: 'blur'
                    }
                ],
                major: [
                    {
                        required: true,
                        message: this.$t('message.inputRequired'),
                        trigger: 'blur'
                    }
                ],
                number: [
                    {
                        required: true,
                        message: this.$t('message.inputRequired'),
                        trigger: 'blur'
                    }
                ]
            },
            createForm: UTILS.clone(defaultCreateForm)
        }
    },
    methods: {
        getOptions() {
            //筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['X_YX', 'profession']
            }
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        faculty: 'X_YX',
                        title: 'profession'
                    },
                    res.data,
                    this.options
                )
            })
            //查询当前学院人数
            this.findFaculty()
            this.searchList()
        },
        findFaculty() {
            this.currencyList = []
            APIs.findTutorNumber(this.currencyType).then(res => {
                if (res.code == 200 && res.data.length > 0) {
                    this.currencyList = res.data
                    this.parameter.faculty = res.data[0].faculty
                    this.parameter.number = res.data[0].number
                    this.parameter.id = res.data[0].id
                }
            })
        },
        findLocalFaculty() {},
        deletelRecord(data) {
            let ids = []
            ids.push(data.id)
            this.createForm.ids = ids
            this.$delConfirm().then(() => {
                APIs.deleteParticularTutorNumber(this.createForm).then(response => {
                    if (response.code == 200) {
                        this.$message({
                            type: 'success',
                            message: this.$t('common.tips.deleteSuccess')
                        })

                        this.searchList()
                    }
                })
            })
        },
        getFaculty(val) {
            const item = this.currencyList.find(data => {
                if (data.faculty == val) {
                    return data
                }
            })
            if (item) {
                this.parameter.number = item.number
                this.parameter.id = item.id
            } else {
                this.parameter.id = null
                this.parameter.number = 1
            }
        },
        close(data) {
            if (data.id) {
                //编辑
                this.closeDialog()
            } else {
                this.dialogVisible = true
            }
        },
        cancle() {
            this.dialogVisible = false
        },
        summit() {
            this.dialogVisible = false
            this.closeDialog()
        },
        saveClose() {
            this.dialogVisible = false
            this.submitForm()
        },
        save() {
            this.$refs.parameter.validate(valid => {
                if (valid) {
                    this.$eduConfirm('您确定要修改吗？', '提示').then(() => {
                        let List = []
                        this.parameter.type = this.currencyType
                        List.push(this.parameter)
                        APIs.addTutorNumber(List).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: this.$t('修改成功')
                                })
                                this.findFaculty()
                            }
                        })
                    })
                }
            })
        },
        reset() {
            this.findFaculty()
        },
        checkTeacherCode(rule, value, callback) {
            if (!value) {
                callback(new Error(this.$t('message.inputRequired')))
            } else if (!this.createForm.teacherName) {
                callback(new Error(this.$t('Parameter.teacherNotExist')))
            } else {
                callback()
            }
        },
        //新增
        getTeacherfo() {
            let type = this.tutorType
            let code = this.createForm.teacherCode
            this.teachers = []
            this.options.major = []
            if (!code) {
                this.createForm = UTILS.clone(defaultCreateForm)
                return
            }
            TutorAPIS.findTutorListByVo(type, code).then(res => {
                if (res.code == 200) {
                    this.teachers = res.data
                    let options = []
                    for (const item of this.teachers) {
                        if (this.tutorType == 1) {
                            options.push({
                                label: item.majorI18n + '-' + item.tutorGrade + '级',
                                value: item.major + '-' + item.tutorGrade
                            })
                        } else {
                            if (item.major == null) {
                                options.push({
                                    label: '全部',
                                    value: null
                                })
                            } else {
                                options.push({
                                    label: item.majorI18n + '(' + item.major + ')',
                                    value: item.major
                                })
                            }
                        }
                    }
                    this.options.major = options
                    if (UTILS.isNotEmpty(this.teachers)) {
                        let item = this.teachers[0]
                        this.createForm.teacherCode = item.teacherCode
                        this.createForm.teacherName = item.teacherName
                        this.createForm.personnelCollege = item.personnelCollege
                        this.createForm.faculty = item.faculty
                        this.createForm.title = item.title
                        this.createForm.school = item.school
                        this.$nextTick(() => {
                            this.createForm.major =
                                this.tutorType == 1
                                    ? item.major + '-' + item.tutorGrade
                                    : item.major
                        })
                        this.createForm.grade = item.tutorGrade
                        this.$refs.createForm.clearValidate()
                    } else {
                        //this.createForm = UTILS.clone(defaultCreateForm)
                        this.createForm.teacherName = ''
                        this.createForm.personnelCollege = ''
                        this.createForm.faculty = ''
                        this.createForm.title = ''
                        this.createForm.school = ''
                        this.createForm.major = ''
                        this.createForm.grade = null
                        this.$refs.createForm.validateField('teacherCode')
                    }
                }
            })
        },
        //编辑
        getMajor() {
            let type = this.tutorType
            let code = this.createForm.teacherCode
            this.teachers = []
            this.options.major = []
            if (type == '1') {
                this.createForm.major = this.createForm.major + '-' + this.createForm.grade
            }
            TutorAPIS.findTutorListByVo(type, code).then(res => {
                if (res.code == 200) {
                    this.teachers = res.data
                    for (const item of this.teachers) {
                        if (this.tutorType == 1) {
                            this.options.major.push({
                                label: item.majorI18n + '-' + item.tutorGrade + '级',
                                value: item.major + '-' + item.tutorGrade
                            })
                        } else {
                            if (item.major == null) {
                                this.options.major.push({
                                    label: '全部',
                                    value: null
                                })
                            } else {
                                this.options.major.push({
                                    label: item.majorI18n + '(' + item.major + ')',
                                    value: item.major
                                })
                            }
                        }
                    }
                }
            })
        },
        getTeacher(isSchool) {
            let item = null
            if (isSchool) {
                let [major, grade] = this.createForm.major.split('-')
                item = this.teachers.find(data => {
                    if (data.major == major && data.tutorGrade == grade) {
                        return data
                    }
                })
            } else {
                let major = this.createForm.major
                item = this.teachers.find(data => {
                    if (data.major == major) {
                        return data
                    }
                })
            }
            if (item) {
                this.createForm.teacherCode = item.teacherCode
                this.createForm.teacherName = item.teacherName
                this.createForm.personnelCollege = item.personnelCollege
                this.createForm.faculty = item.faculty
                this.createForm.title = item.title
                this.createForm.school = item.school
                this.createForm.grade = item.tutorGrade
                this.createForm.major = isSchool ? item.major + '-' + item.tutorGrade : item.major
            } else {
                this.createForm.faculty = ''
                this.createForm.school = ''
            }
        },
        updateRecord(data) {
            if (!data) {
                //新增
                this.isShow = false
                this.dialogTitle = this.$t('message.add')
                this.createForm = UTILS.clone(defaultCreateForm)
                this.options.major = []
            } else {
                this.isShow = true
                //通过teacherCode查找，获取专业列表
                this.createForm = data
                this.getMajor()
                this.dialogTitle = this.$t('message.edit')
            }
            this.$refs.updateDialog.open()
        },
        searchList() {
            this.queryParams.type = this.tutorType
            this.$refs.mainTable.search(this.queryParams)
        },
        resetAddForm() {
            this.$refs.createForm.clearValidate()
            this.searchList()
        },
        closeDialog() {
            this.$refs.updateDialog.close()
            this.$refs.createForm.clearValidate()
            this.searchList()
        },
        submitForm() {
            this.$refs.createForm.validate(valid => {
                if (valid) {
                    let item = UTILS.clone(this.createForm)
                    item.type = this.tutorType
                    if (item.type == 1) {
                        item.major = item.major.split('-')[0]
                    }
                    if (item.id) {
                        //编辑
                        APIs.editParticularTutorNumber(item).then(response => {
                            if (response.code == 200) {
                                let message = this.$t('message.editSuccess')
                                this.$message({
                                    type: 'success',
                                    message: message
                                })
                                this.closeDialog()
                            }
                        })
                    } else {
                        //新增
                        APIs.addParticularTutorNumber(item).then(response => {
                            if (response.code == 200) {
                                let message = this.$t('message.addSuccess')
                                this.$message({
                                    type: 'success',
                                    message: message
                                })
                                this.closeDialog()
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>
